//
//  JKSuspensionView.m
//  JKFloatingBox
//
//  Created by 王冲 on 2019/9/18.
//  Copyright © 2019 王冲. All rights reserved.
//

#import "JKSuspensionView.h"

#import "UIView+Extension.h"
#define SuspensionBtnTouchWidth self.frame.size.width
#define SuspensionTouchHeight self.frame.size.height
#define SuspensionScreenWidth [UIScreen mainScreen].bounds.size.width
#define SuspensionScreenHeight [UIScreen mainScreen].bounds.size.height
@interface JKSuspensionView ()

@property (nonatomic, assign) CGFloat spaceHorizontal;

@end
@implementation JKSuspensionView

- (instancetype)initWithFrame:(CGRect)frame color:(UIColor *)color withSpace:(CGFloat)space {
    if(self = [super initWithFrame:frame])
    {
        self.spaceHorizontal = space;
        self.userInteractionEnabled = YES;
        self.backgroundColor = color;
        self.alpha = 1.0;
        [self cy_addShadowToViewShadowRadius:4 withCornerRadius:SuspensionTouchHeight/2.0 withColor:[UIColor blackColor] withShadowOffset:CGSizeMake(0, 0) withShadowOpacity:0.48 withIsClipsCorner:YES];
        //拖动
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(changeLocation:)];
        pan.delaysTouchesBegan = YES;
        [self addGestureRecognizer:pan];
        //点击
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click)];
        [self addGestureRecognizer:tapGestureRecognizer];
    }
    return self;
}

#pragma mark - event response
- (void)changeLocation:(UIPanGestureRecognizer*)panGestureRecognizer {
    UIWindow *appWindow = [UIApplication sharedApplication].delegate.window;
    CGPoint panPoint = [panGestureRecognizer locationInView:appWindow];
    
    //透明度
    if(panGestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
        self.alpha = 1;
    }
    else if (panGestureRecognizer.state == UIGestureRecognizerStateEnded)
    {
        self.alpha = 1;
    }
    
    //位置
    if(panGestureRecognizer.state == UIGestureRecognizerStateChanged){
        
        self.center = CGPointMake(panPoint.x, panPoint.y);
        [self cy_addShadowToViewShadowRadius:4 withCornerRadius:SuspensionTouchHeight/2.0 withColor:[UIColor blackColor] withShadowOffset:CGSizeMake(0, 0) withShadowOpacity:0.48 withIsClipsCorner:YES];
        //[self cy_cutRoundRectCorner:UIRectCornerAllCorners withCornerRadius:SuspensionTouchHeight/2.0];
        
    }else if(panGestureRecognizer.state == UIGestureRecognizerStateEnded){
        
        CGFloat left = fabs(panPoint.x);
        CGFloat right = fabs(SuspensionScreenWidth - left);
        //ripper:只停留在左右，需要在上下，将注释打开
        //  CGFloat top = fabs(panPoint.y);
        //  CGFloat bottom = fabs(SuspensionScreenHeight - top);
        
        CGFloat minSpace = MIN(left, right);
        //  CGFloat minSpace = MIN(MIN(MIN(top, left), bottom), right);
        CGPoint newCenter;
        CGFloat targetY = 0;
        
        //校正Y
        if (panPoint.y < 15 + SuspensionTouchHeight/2.0) {
            targetY = 15 + SuspensionTouchHeight/2.0;
        }else if (panPoint.y > (SuspensionScreenHeight - SuspensionTouchHeight/2.0 - 15)) {
            targetY = SuspensionScreenHeight - SuspensionTouchHeight/2.0 - 15;
        }else{
            targetY = panPoint.y;
        }
        
        if (minSpace == left) {
            newCenter = CGPointMake(SuspensionTouchHeight/3 + 10+self.spaceHorizontal, targetY);
        }else{
            newCenter = CGPointMake(SuspensionScreenWidth-SuspensionTouchHeight/3-10-self.spaceHorizontal, targetY);
        }

        [self cy_addShadowToViewShadowRadius:4 withCornerRadius:SuspensionTouchHeight/2.0 withColor:[UIColor blackColor] withShadowOffset:CGSizeMake(0, 0) withShadowOpacity:0.48 withIsClipsCorner:YES];
        
        [UIView animateWithDuration:.25 animations:^{
            self.center = newCenter;
        }];
    }
}

- (void)click {
    //关闭所有的键盘
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    if([self.delegate respondsToSelector:@selector(suspensionViewClick:)])
    {
        [self.delegate suspensionViewClick:self];
    }
}

/** 显示悬浮球 */
- (void)showSuspension {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

/** 移除悬浮球 */
- (void)removeSuspension {
    [self removeFromSuperview];
}

@end
